﻿using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

namespace Silent.API.LastFm.Requests
{
    public class AlbumSearchRequest : RequestBase<List<Album>>
    {
        public AlbumSearchRequest(string apiKey, string album)
            : base(apiKey, QueryConstants.ALBUM_SEARCH)
        {
            parameters.AddRange(new[] {
                new QueryParameter(QueryConstants.PREFIX_ALBUM, album)
            });
        }
        public AlbumSearchRequest(string apiKey, string artist, string album)
            : base(apiKey, QueryConstants.ALBUM_SEARCH)
        {
            parameters.AddRange(new[] {
                new QueryParameter(QueryConstants.PREFIX_ARTIST, artist),
                new QueryParameter(QueryConstants.PREFIX_ALBUM, album)
            });
        }

        public override List<Album> Request()
        {
            var query = new QueryBuilder().BuildQuery(parameters.ToArray());
            var elements = XElement.Load(query).Descendants("album");
            var objects = elements.Select(Converter.ConvertToAlbum).ToList();
            return objects;
        }
    }
}